<?php
/**
 * Created by PhpStorm.
 * User: Lee
 * Date: 2020/1/2
 * Time: 16:39
 */
namespace App\Http\Controllers\System;

use App\Http\Controllers\Controller;
use App\Common\Utils\JsonResponse;
use App\Http\Request\System\User\ListRequest;
use App\Http\Request\System\User\SetuserroleRequest;
use App\Http\Request\System\User\SetuserstatusRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx\Rels;

class UserController extends Controller
{
    /** @var \App\Services\System\UserService $userService */
    private $userService;
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this->middleware('auth');
        $this->userService = app('userService');
    }

    //用户角色管理页面
    public function index()
    {
        return view('systems.users.index');
    }

    /**
     * 获取用户列表
     */
    public function getList(ListRequest $request)
    {
        try
        {
            if(!$request->isValid()){
                throw new \Exception($request->getErrMsg());
            }else{
                $requestData = $request->getData();
                $result = $this->userService->getList($requestData);
                return JsonResponse::get($result['data'],TRUE,$result['total']);
            }
        }
        catch(\Exception $e)
        {
            return $this->error($e);
        }
    }

    //获取所有角色
    public function getAllRole()
    {
        try
        {
            $result = $this->userService->getAllRole();
            return JsonResponse::get($result);
        }
        catch(\Exception $e)
        {
            return $this->error($e);
        }
    }

    /**
     * 设置用户角色
     */
    public function setUserRole(SetuserroleRequest $request)
    {
        try
        {
            if(!$request->isValid()){
                throw new \Exception($request->getErrMsg());
            }else{
                $requestData = $request->getData();
                $this->userService->setUserRole($requestData);
                $logService = app('logService');
                $logService->log('162','设置用户角色', "设置用户角色，用户编号[{$requestData['id']}]");
                return JsonResponse::success();
            }
        }
        catch(\Exception $e)
        {
            return $this->error($e);
        }
    }

    /**
     * 启用/禁用用户
     */
    public function setUserStatus(SetuserstatusRequest $request)
    {
        try
        {
            if(!$request->isValid()){
                throw new \Exception($request->getErrMsg());
            }else{
                $requestData = $request->getData();
                $this->userService->setUserStatus($requestData);
                $logService = app('logService');
                $userids = implode(",",$requestData['id']);
                if($requestData['status'] == 1){
                    $logService->log('161','启用用户账号', "启用用户账号，用户编号[{$userids}]");
                }else{
                    $logService->log('160','禁用用户账号', "禁用用户账号，用户编号[{$userids}]");
                }
                return JsonResponse::success();
            }
        }
        catch(\Exception $e)
        {
            return $this->error($e);
        }
    }

    /**
     * 获取用户列表
     */
    public function getGroupTree()
    {
        try
        {
            $result = $this->userService->getGroupTree();
            return JsonResponse::get($result);
        }
        catch(\Exception $e)
        {
            return $this->error($e);
        }
    }

    /**
     * 获取菜单
     * @author lxy
     * @datetime 2022-11-17
     */
    public function getUserMenus(Request $request)
    {
        try
        {
            $result = $this->userService->getUserMenus($request->all());

            return $result;
        }
        catch(\Exception $e)
        {
            return $this->error($e);
        }
    }

    /**
     * @Description: 设置用户当前角色
     * @Author: kzj
     * @datetime: 2022-12-01
     */
    public function setUserCurRole(Request $request)
    {
        try
        {
            $result = $this->userService->setUserCurRole($request->all());

            return JsonResponse::get($result);
        }
        catch(\Exception $e)
        {
            return $this->error($e);
        }
    }

    /**
     * @Description: 更新用户信息
     * @Author: kzj
     * @datetime: 2023-02-07
     */
    public function updateInfo(Request $request)
    {
        try{
            $result = $this->userService->updateInfo($request->all());
            // $logService = app('logService');
            // $logService->log('','更新用户信息', "更新用户信息");
            return JsonResponse::success($result,'操作成功');
        }catch(\Exception $e){
            Log::error($e);
            return JsonResponse::error($e->getMessage());
        }
    }
}
